<?php
class CourseAction extends CommonAction {
	
	/**
	 * 删除
	 * @param unknown_type $id
	 */
	public function del($id) {
		$result = false;
// 		$course = M ( "Course" );
		//$result = $Student->Where('id='.$id )->delete();
// 		$result = $course->delete($id);
		$map =  array ();
		$errmsg = "删除出错！";
		$total = 0;
		$map["LS_KCBH"] = $id;
		$orderList = M ("OrderList");
		$total = $orderList->where($map)->count (); // 计算总数
		
		if($total == 0){
			$result = $this->deleteById($id);
		}else{
			$errmsg = "该课程已经有学生报名不能删除！";
		}
		if ($result == false) {
			echo json_encode (array(
					'errorMsg' => $errmsg
			));
		} else {
			echo json_encode (array(
					'success' => true 
			));
		}
	}
	
	public function editBefore(){
		$id = $_GET["id"];
		$course = M ("Course");
		$vo = $course->getById($id);
		echo json_encode($vo);
	}
	
	/**
	 * 修改
	 * @param 
	 */
	public function save() {
		$result = false;
		$id = $_GET["id"];
		$course = D ("Course");
		if($course->create()) {
			$result = $course->where('id='.$_REQUEST['id'] )->save();
			if($result || $result==0) {
				echo json_encode (array('success'=>true));
			}else{
				echo json_encode (array('errorMsg'=>'修改出错！'));
			}
		}else{
			echo json_encode (array('errorMsg'=>$course->getError()));
		}
	}
	
	/**
	 * 新增
	 */
	public function add() {
		$result = false;
		$course = D("Course");
		if($course->create()) {
			$result = $course->add();
			if($result) {
				echo json_encode (array('success'=>true));
			}else{
				echo json_encode (array('errorMsg'=>'添加出错！'));
			}
		}else{
			echo json_encode (array('errorMsg'=>$course->getError()));
			//$this->error($course->getError());
		}

	}
	
	/**
	 * 查询(non-PHPdoc)
	 * @see CommonAction::read()
	 */
	public function read() {
		$map =  array ();
		
		$pagenum = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rowsnum = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 15;
		$course = M ( "Course" );
		$total = $course->where($map)->count (); // 计算总数	

		$courseList = $course->where($map)->limit (($pagenum - 1) * $rowsnum . ',' . $rowsnum )->order ( 'id asc' )->select ();
		$resultJson = json_encode($courseList);
		if($resultJson == "null"){//如果查询结果返回为null
			$resultJson = "[]";
		}
		$json = '{"total":' . $total . ',"rows":' . $resultJson . '}'; // 重要，easyui的标准数据格式，数据总数和数据内容在同一个json中		
		echo $json;
	}
	
	public function getDisplayList(){
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
		$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 15;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'KC_NJ';
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
		
		$xqid =  $_POST["XQ"];
// 		if(isset($_SESSION['xqid'])) {
// 			$xqid =$_SESSION['xqid'];
// 		}
		switch ($sort)
		{
			case "XQ":
				$sort = "KC_XQ";
				break;
			case "NJ":
				$sort = "KC_NJ";
				break;
			case "KM":
				$sort = "KC_KMBH";
				break;
			default:
				$sort = "KC_NJ";
		}

		if(empty($xqid)){
			$xqid =  $_SESSION['XqFilterString'];
		}else{
			$xqid = "'" . $xqid ."'";
		}
		
		$kcmc = $_POST['KCMC'];
		$km = $_POST['KM'];
		
		$offset = ($page-1)*$rows;
		$course = D ( "Course" );
		$result = $course->getModeDisplayList($xqid,$kcmc,$km,$offset,$rows,$sort,$order);
		echo json_encode($result);
	}
	
// 	public function getDictList(){
// // 		$flValue = $_REQUEST ['flValue'];
// 		$flValue = $_GET["flValue"];
// 		$dicList = D( "DictionaryList" );
// 		$dictList = $dicList->getDictList($flValue);
// 		$resultJson = json_encode($dictList);
// 		echo $resultJson;
// 	}
	

}
?>
